﻿using Ecommerce.Domain;
using Ecommerce.Domain.User;
using Ecommerce.Repository.Interface.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ecommerce.Services.User
{
    public class UserInfoServices : IUseInfoServices
    {
        private readonly IUserInfoRepository userInfoRepository;

        private readonly IRoleRepository roleRepository;

        private readonly IProvinceRepository provinceRepository;

        private readonly ICityRepository cityRepository;

        private readonly ICountyRepository countyRepository;

        private readonly IAccountRepository accountRepository;

        public UserInfoServices(IRoleRepository roleRepository, IProvinceRepository provinceRepository, ICityRepository cityRepository, ICountyRepository countyRepository, IUserInfoRepository userInfoRepository, IAccountRepository accountRepository)
        {
            this.roleRepository = roleRepository;
            this.provinceRepository = provinceRepository;
            this.cityRepository = cityRepository;
            this.countyRepository = countyRepository;
            this.userInfoRepository = userInfoRepository;
            this.accountRepository = accountRepository;
        }
        public async Task<List<UseInfo>> GetLogin(string Name, string UserPaw)
        {
            var list = await userInfoRepository.GetAsync(w => w.Name == Name && w.UserPaw==UserPaw);
            //if (list.Count() == 0)
            //{
            //    return -1;
            //}
            //else
            //{
            //    if(list.FirstOrDefault().UserPaw!=UserPaw)
            //    {
            //        return -2;
            //    }
            //    return list;
            //}
            return list;
        }

        public async Task<List<Role>> GetRoles()
        {
            return await roleRepository.GetAllAsync();
        }

        public async Task<object> GetUser(int Pid)
        {
            var list = await userInfoRepository.GetAsync(w => w.Pid == Pid);
            var Plist = list.ToList().GroupBy(w => new { w.Name }).Select(w => new { w.Key.Name });
            return Plist;
        }
        public async Task<object> GetAccount()
        {
            var list = from a in await accountRepository.GetAllAsync()
                       join b in await countyRepository.GetAllAsync() on a.Tid equals b.Tid
                       join c in await cityRepository.GetAllAsync() on b.Uid equals c.Uid
                       join d in await provinceRepository.GetAllAsync() on c.Cid equals d.Cid
                       join e in await roleRepository.GetAllAsync() on a.Rid equals e.Rid
                       select new
                       {
                           a.UseId,
                           a.UserName,
                           a.UseSex,
                           a.Address,
                           b.Tid,
                           b.CountName,
                           c.Uid,
                           c.BanName,
                           d.CityName,
                           d.Cid,
                           e.RidName,
                           e.Rid
                       };
            return list;
        }


        public async Task<List<Account>> GetNameUser(string Name)
        {
            return await accountRepository.GetAsync(w => w.UserName == Name);
        }

        public async Task<int> AddAccount(Account account)
        {
            account.Address = account.Address;
            account.Tid = account.Tid;
            account.Rid = account.Rid;
            account.UserName = account.UserName;
            account.UseSex = account.UseSex;
            return await accountRepository.AddAsync(account);
        }

        public async Task<List<Account>> AccountLish(int rid)
        {
            return await accountRepository.GetAsync(w => w.Rid == rid);
        }

        public async Task<int> UpdateAccount(Account account)
        {
            account.Tid = account.Tid;
            account.Rid = 0;
            account.UserName = account.UserName;
            account.Address = account.Address;
            account.UseSex = account.UseSex;
            account.UseId = account.UseId;
            return await accountRepository.Update(account);
        }
    }
}
